import Promise from 'promise';
import * as tools from '../common/tools';
import * as at from '../common/at';
import * as ResultMsg from '../constrants/ResultMsg';
import { ReplyModel } from '../models';

export function count(appId, conditions, options) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof conditions !== 'object') throw new Error(ResultMsg.NOT_OBJECT);
  if (typeof options !== 'object') options = null;
  conditions.appId = appId;
  return ReplyModel.count(conditions).setOptions(options);
}

export function find(appId, conditions, options) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof conditions !== 'object') throw new Error(ResultMsg.NOT_OBJECT);
  if (typeof options !== 'object') options = null;
  conditions.appId = appId;
  const query = ReplyModel.find(conditions).setOptions(options);
  return query.exec();
}

export function findOne(appId, conditions, options) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof conditions !== 'object') throw new Error(ResultMsg.NOT_OBJECT);
  if (typeof options !== 'object') options = null;
  conditions.appId = appId;
  const query = ReplyModel.findOne(conditions).setOptions(options);
  return query.exec();
}

export function findOneById(appId, id) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  const query = ReplyModel.findOne({appId: appId, _id: id });
  return query.exec();
}

export function findByTopicId(appId, id) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  const query = ReplyModel.find({appId: appId, topic_id: id, deleted: false }).setOptions({ sort: 'create_at' });
  return query.exec();
}

export function update(appId, id, data) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  if (typeof data !== 'object') throw new Error(ResultMsg.NOT_OBJECT);
  const query = ReplyModel.findByIdAndUpdate(id, { $set: data });
  return query.exec();
}

export function create(appId, {content, topicId, authorId, replyId }) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof content !== 'string') throw new Error(ResultMsg.NOT_STRING);
  if (typeof topicId !== 'string' && typeof topicId !== 'object') throw new Error(ResultMsg.NO_ID);
  if (typeof authorId !== 'string' && typeof authorId !== 'object') throw new Error(ResultMsg.NO_ID);
  if (typeof replyId !== 'string' && typeof replyId !== 'object') replyId = '';
  const doc = new ReplyModel();
  doc.appId = appId;
  doc.content = content;
  doc.topic_id = topicId;
  doc.author_id = authorId;

  if (replyId) doc.reply_id = replyId;

  return doc.save();
}

export function getLastReplyIdByTopicId(appId, id) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  const query = ReplyModel.findOne({appId: appId, topic_id: id, deleted: false }).select('_id').setOptions({ sort: '-create_at' });
  return query.exec();
}

export function getRepliesByAuthorId(appId, id, options) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  if (typeof options !== 'object') options = null;
  const query = ReplyModel.find({appId: appId, author_id: id }).setOptions(options);
  return query.exec();
}

export function getCountByAuthorId(appId, id) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  return ReplyModel.count({appId: appId, author_id: id });
}
